<?php

/**
 * JqGridView class file.
 *
 */
Yii::import('zii.widgets.jui.CJuiWidget');
Yii::import('application.extensions.jqgrid.JqGridColumnsModel');
Yii::import('application.extensions.jqgrid.JqGridNavigator');
Yii::import('application.extensions.jqgrid.JqGridFilterToolbar');


class JqGridView extends CJuiWidget {

    private $_formatter;
    public $htmlOptions = array();
    public $tagName = 'table';
    public $dataProvider;
    public $columns = array();
    public $baseScriptUrl;
    public $cssFile;
    public $nullDisplay = '&nbsp;';
    public $lang = 'en'; // @TODO lang from locales
    public $options;
    public $GridNavigator;
    public $GridFilterToolbar;
    public $columnsModel;
    protected $data = array();
    


    /**
     * Initializes the grid view.
     * This method will initialize required property values and instantiate {@link columns} objects.
     */
    public function init() {
        $this->htmlOptions['id'] = $this->getId();

//        if (!isset($this->htmlOptions['class']))
//            $this->htmlOptions['class'] = 'grid-view';

        $this->initColumns();
        $this->initData();
        $this->initNavigator();
        $this->initFilterToolbar();
    }

    public function run() {

        echo CHtml::openTag($this->tagName, $this->htmlOptions) . "\n";
        echo CHtml::closeTag($this->tagName);

        $this->resolvePackagePath();
        $this->registerCoreScripts();
        $this->registerClientScript();
    }

    /**
     * Creates column objects and initializes them.
     */
    protected function initColumns() {

        $this->columnsModel = Yii::createComponent($this->columnsModel, $this);
        $this->columnsModel->init();
    }

    protected function initData() {
        if(!isset($this->options['datatype']))$this->options['datatype'] = 'local';
        elseif($this->options['datatype'] != 'local')return;

        $data = $this->dataProvider->getData();
        
        $n = count($data);

        if ($n > 0) {
            for ($row = 0; $row < $n; ++$row) {
                $grid_row = array();

                foreach ($this->columns as $column) {
                    $grid_row[$column->name] = $column->getCellData($row);
                }
                $this->data[] = $grid_row;
            }
        }
        //print_r($this->data);
    }

    protected function initNavigator(){
        if(!is_array($this->GridNavigator))return;
        $this->GridNavigator['class'] = 'JqGridNavigator';
        $this->GridNavigator = Yii::createComponent($this->GridNavigator, $this);
    }

    protected function initFilterToolbar(){
        if(!is_array($this->GridFilterToolbar))return;
        $this->GridFilterToolbar['class'] = 'JqGridFilterToolbar';
        $this->GridFilterToolbar = Yii::createComponent($this->GridFilterToolbar, $this);
    }

    /**
     * Registers necessary client scripts.
     */
    public function registerClientScript() {
        $id = $this->getId();

        $options = array();

        $options['colNames'] = $this->columnsModel->colNames;
        $options['colModel'] = $this->columnsModel->colModel;
        $options['data'] = $this->data;

        if(is_array($this->options))$options = array_merge($options, $this->options);

        if($this->options['pager']){
            echo CHtml::openTag('div', array('id'=>  str_replace("#", "", $this->options['pager']))) . "\n";
            echo CHtml::closeTag('div');
        }
        if (!isset($this->options['imgpath']))
            $this->options['imgpath'] = $this->themeUrl.'/'.$this->theme.'/images/';

        $options = CJavaScript::encode($options);
        $cs = Yii::app()->getClientScript();

        $baseUrl = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . '/assets'/* , false, -1, true */);
        $cs->registerCssFile($baseUrl.'/css/ui.jqgrid.css');

        $cs->registerScriptFile($baseUrl . '/js/i18n/grid.locale-'.$this->lang.'.js');
        $cs->registerScriptFile($baseUrl . '/js/jquery.jqGrid.min.js');

        $cs->registerScript(__CLASS__ . '#' . $id, "jQuery('#$id').jqGrid($options);");
        if($this->GridNavigator instanceof JqGridNavigator)$this->GridNavigator->registerClientScript();
        if($this->GridFilterToolbar instanceof JqGridFilterToolbar)$this->GridFilterToolbar->registerClientScript();
            
    }

    public function getFormatter() {
        if ($this->_formatter === null)
            $this->_formatter = Yii::app()->format;
        return $this->_formatter;
    }

}
